package client;
import common.*;

//This is a buffer to hold a constant number of chat messages.
//For use in a chatroom application.

public class ChatBuffer {
	private String [] buf;
	private int bufStart = 0;
	private int bufEnd = 1;
	
	public ChatBuffer (int i) {
		buf = new String [i+1];
		String s = " ";
		for (int j = 0; j<=i; ++j) buf[j]=s;
	}
	public void add (String s) {
		buf[bufEnd] = s;
		bufEnd = (bufEnd+1)%buf.length;
		if (bufEnd == bufStart) 
			bufStart = (bufStart+1)%buf.length;
	}
	public void add (ChatPacket c) {
		this.add(c.toString());
	}
	public void clear() {
		for (; bufStart != bufEnd; bufStart=(bufStart+1)%buf.length) buf[bufStart] = " ";
	}
	
	public String toString() {
		String s = "";//"<html>";
		for (int i = bufStart; i != bufEnd; i=(i+1)%buf.length) 
			s = s + buf[i] + "\n";//<br>";
		s = s + "";//</html>";
		
		return s;
	}
}
